﻿using SigowayCommonLib;
using SigowayCommonLib.ADO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using WarehouseManager.Common;

namespace WarehouseManager
{
    public partial class OperatorSettingForm : Form
    {
        private const string UpdateMessage = "可修改姓名，点击更新按钮进行保存。";
        private const string ErrorMessage = "系统管理员账户信息不允许修改！";
        private const string InsertMessage = "该操作员信息不存在，输入姓名之后，点击保存按钮进行添加。";

        private const string UpdateText = "更新";
        private const string AddText = "添加";

        private DBManager dbManager;
        private string _operatorID = string.Empty;

        public OperatorSettingForm(string operatorID, string operatorName)
        {
            InitializeComponent();
            Init(operatorID, operatorName);
        }

        private void Init(string operatorID, string operatorName)
        {
            this._operatorID = operatorID;
            this.lblOperator.Text = operatorName;
            this.lblOperatDate.Text = DateTimeUtil.GetCurrentDate();
            this.txtID.Focus();
            this.dbManager = DBManager.GetDBManager();
        }

        private void txtID_Validating(object sender, CancelEventArgs e)
        {
            this.lblMsg.Visible = false;
            if (String.IsNullOrEmpty(this.txtID.Text))
            {
                this.ClearName();
                return;
            }
            this.errorProvider.Clear();
            this.txtName.ReadOnly = false;
            this.btnSave.Enabled = true;

            try
            {
                var sqlText = string.Format("SELECT OperatorName FROM Operator WHERE OperatorID = '{0}'", this.txtID.Text);

                object objName = this.dbManager.GetSingleValue(new SQLObject(sqlText, null));
                if (objName != null)
                {
                    if (this.txtID.Text != ConstInfo.Admin || this._operatorID == ConstInfo.Admin)
                    {
                        this.txtName.Text = objName.ToString();
                        this.lblMsg.Text = UpdateMessage;                    
                        this.btnSave.Text = UpdateText;
                    }
                    else
                    {
                        this.txtName.Text = objName.ToString();
                        this.lblMsg.Text = ErrorMessage;
                        this.txtName.ReadOnly = true;
                        this.btnSave.Enabled = false;
                    }
                }
                else
                {
                    this.ClearName();
                    this.lblMsg.Text = InsertMessage;
                    this.btnSave.Text = AddText;
                }

                this.txtName.Focus();
                this.lblMsg.Visible = true;
            }
            catch(Exception ex)
            {
                MessageBoxHelp.ShowMsgError("操作员信息查询失败!");
                LogManager.WriteLine("操作员信息查询失败!Exception={0}", ex);
            }
        }

        private void ClearName()
        {
            this.txtName.Text = string.Empty;
        }

        private void txtName_Validating(object sender, CancelEventArgs e)
        {
            if (!String.IsNullOrEmpty(this.txtName.Text))
            {
                this.errorProvider.Clear();
                return;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txtID.Text))
            {
                this.errorProvider.SetError(this.txtID, "请输入操作员ID");
                return;
            }

            if (String.IsNullOrEmpty(this.txtName.Text))
            {
                this.errorProvider.SetError(this.txtName, "请输入操作员姓名");
                return;
            }

            try
            {
                var sqlText = this.btnSave.Text == AddText 
                                ? "INSERT INTO Operator VALUES(?,?,?,?,?,?,?)" 
                                : "UPDATE Operator SET OperatorName = ?, UpdateUser = ?, UpdateDate = ? WHERE OperatorID = ?";

                IDictionary<string, object> dicParameter = new Dictionary<string, object>();
               
                if (this.btnSave.Text == AddText)
                {
                    dicParameter.Add("OperatorID", this.txtID.Text);
                    dicParameter.Add("OperatorName", this.txtName.Text);
                    dicParameter.Add("CreateUser", this.txtID.Text);
                    dicParameter.Add("CreateDate", DateTimeUtil.GetCurrentDate());
                    dicParameter.Add("UpdateUser", DBNull.Value);
                    dicParameter.Add("UpdateDate", DBNull.Value);
                    dicParameter.Add("pwd", DBNull.Value);
                }
                else
                {
                    dicParameter.Add("OperatorName", this.txtName.Text);
                    dicParameter.Add("UpdateUser", this._operatorID);
                    dicParameter.Add("UpdateDate", DateTimeUtil.GetCurrentDate());
                    dicParameter.Add("OperatorID", this.txtID.Text);
                }
                int result = this.dbManager.ExecuteNonQuery(new SQLObject(sqlText, dicParameter));

                if (result <= 0)
                { 
                    MessageBoxHelp.ShowMsgError(string.Format("操作员信息{0}失败!", this.btnSave.Text));
                }
                else
                {
                    MessageBoxHelp.ShowMsgInfor(string.Format("操作员信息{0}成功!", this.btnSave.Text));
                    this.txtID.Focus();
                    this.txtID.Text = string.Empty;

                    this.txtName.Text = string.Empty;
                    this.lblMsg.Visible = false;

                    this.btnSave.Text = AddText;

                    if (this.txtID.Text == this._operatorID)
                    {
                        this.lblOperator.Text = this.txtName.Text;
                    }

                }
            }
            catch (Exception ex)
            {
                string errMsg = string.Format("操作员信息{0}失败!", this.btnSave.Text);
                MessageBoxHelp.ShowMsgError(errMsg);
                LogManager.WriteLine(errMsg +"\r\nException={0}", ex);
            }
        }
    }
}
